package com.gzx.bi.modular.repayment.subsidiary.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.bi.modular.repayment.subsidiary.bo.GzxRepaymentSubsidiaryAddOrEditBo;
import com.gzx.bi.modular.repayment.subsidiary.bo.GzxRepaymentSubsidiaryQueryBo;
import com.gzx.bi.modular.repayment.subsidiary.entity.GzxRepaymentSubsidiary;
import com.gzx.bi.modular.repayment.subsidiary.mapper.GzxRepaymentSubsidiaryMapper;
import com.gzx.bi.modular.repayment.subsidiary.service.GzxRepaymentSubsidiaryService;
import com.gzx.bi.modular.repayment.subsidiary.vo.GzxRepaymentSubsidiaryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/4/14 14:46
 */
@Service
@DS("bi")
public class GzxRepaymentSubsidiaryServiceImpl extends BaseServiceImpl<GzxRepaymentSubsidiaryAddOrEditBo, GzxRepaymentSubsidiaryVo, GzxRepaymentSubsidiaryQueryBo, GzxRepaymentSubsidiaryMapper, GzxRepaymentSubsidiary> implements GzxRepaymentSubsidiaryService {
    public GzxRepaymentSubsidiaryServiceImpl(GzxRepaymentSubsidiaryMapper baseMapper) {
        super(baseMapper);
    }

    /**
     * 插入一个新的业务对象。
     *
     * @param repaymentSubsidiaryAddOrEditBo 业务对象
     */
    @Override
    public void insert(GzxRepaymentSubsidiaryAddOrEditBo repaymentSubsidiaryAddOrEditBo) {
        GzxRepaymentSubsidiary repaymentSubsidiary = baseMapper.selectOne(Wrappers.<GzxRepaymentSubsidiary>lambdaQuery()
                .eq(GzxRepaymentSubsidiary::getRepaymentDate, repaymentSubsidiaryAddOrEditBo.getRepaymentDate())
                .eq(GzxRepaymentSubsidiary::getProjectCode, repaymentSubsidiaryAddOrEditBo.getProjectCode())
                .eq(GzxRepaymentSubsidiary::getBatch, repaymentSubsidiaryAddOrEditBo.getBatch())
                .eq(GzxRepaymentSubsidiary::getDeptId, repaymentSubsidiaryAddOrEditBo.getDeptId()));
        if (repaymentSubsidiary != null) {
            repaymentSubsidiary.setRepaymentAmount(repaymentSubsidiaryAddOrEditBo.getRepaymentAmount());
            baseMapper.updateById(repaymentSubsidiary);
        } else {
            super.insert(repaymentSubsidiaryAddOrEditBo);
        }
    }

    @Override
    protected LambdaQueryWrapper<GzxRepaymentSubsidiary> buildQueryWrapper(GzxRepaymentSubsidiaryQueryBo queryBo) {
        return Wrappers.<GzxRepaymentSubsidiary>lambdaQuery()
                .eq(StringUtils.isNotBlank(queryBo.getProjectCode()), GzxRepaymentSubsidiary::getProjectCode, queryBo.getProjectCode())
                .eq(queryBo.getDeptId() != null, GzxRepaymentSubsidiary::getDeptId, queryBo.getDeptId())
                .between(StringUtils.isNotBlank(queryBo.getBeginTime()) && StringUtils.isNotBlank(queryBo.getEndTime()), GzxRepaymentSubsidiary::getRepaymentDate, queryBo.getBeginTime(), queryBo.getEndTime())
                .orderByDesc(GzxRepaymentSubsidiary::getId);
    }

    /**
     * 查询分公司上报的还款数据
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public List<GzxRepaymentSubsidiary> selectList(LambdaQueryWrapper<GzxRepaymentSubsidiary> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }
}
